package com.pentofab.payrollmanagement.dao.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import com.pentofab.payrollmanagement.dao.LeaveDAO;
import com.pentofab.payrollmanagement.dao.model.Holiday;
import com.pentofab.payrollmanagement.dao.model.Leave;
import com.pentofab.payrollmanagement.database.LeaveRepository;
import com.pentofab.payrollmanagement.database.impl.LeaveRepositoryImpl;
import com.pentofab.payrollmanagement.exception.BusinessException;
import com.pentofab.payrollmanagement.utilities.Constants;
import com.pentofab.payrollmanagement.utilities.PayRollUtility;

public class LeaveDAOImpl implements LeaveDAO {

	LeaveRepository leaveRepository = null;

	public LeaveDAOImpl() {

		leaveRepository = new LeaveRepositoryImpl();
	}

	public float getLeaveBalance(String empId, int leaveTypeIndex) throws BusinessException {
		
		String leaveType = "";
		if(leaveTypeIndex == 0){
			leaveType = Constants.TYPE_CL;
		}else if(leaveTypeIndex == 1){
			leaveType = Constants.TYPE_SL;
		}else if(leaveTypeIndex == 2){
			leaveType = Constants.TYPE_EL;
		}else if(leaveTypeIndex == 3){
			leaveType = Constants.TYPE_RH;
		}else{
			return 9999;
		}
		try{
			PayRollUtility.debug("leaveType : "+leaveType);
			return leaveRepository.getLeaveBalance(empId, leaveType);
		}catch(Exception e){
			throw new BusinessException(ERROR_WHILE_GETTING_LEAVE_BALANCE);
		}
	}

	public void applyforLeave(Leave leaveObject) throws BusinessException {

		try{
			
			LeaveModuleUtility.validateLeaveObj(leaveObject);
			leaveRepository.applyforLeave(leaveObject);
		}catch(Exception e){
			e.printStackTrace();
			throw new BusinessException(ERROR_WHILE_APPLYING_LEAVE);
		}
	}

	@Override
	public ArrayList<Leave> getLeaveHistory(String empId) throws BusinessException {
		try {

			return leaveRepository.getLeaveHistory(empId);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(ERROR_DURING_LEAVE_HISTORY_RETRIVAL+empId);
		}
	}

	@Override
	public void deleteLeave(String empId, String from_DATE, String to_DATE)
			throws BusinessException {

		if (!LeaveModuleUtility.isLeaveDeletable(to_DATE)) {
			throw new BusinessException(CANCEL_NOT_ALLOWED);
		}
		try {

			leaveRepository.deleteLeave(empId, from_DATE, to_DATE);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(ERROR_DURING_LEAVE_HISTORY_RETRIVAL
					+ empId);
		}

	}

	@Override
	public boolean empOnLeave(String empId, String date) throws BusinessException {

		try {

			return leaveRepository.empOnLeave(empId, date);
		} catch (Exception e) {
			e.printStackTrace();
			throw new BusinessException(ERROR_DURING_LEAVE_HISTORY_RETRIVAL
					+ empId);
		}
		
	}

	@Override
	public ArrayList<Holiday> getHolidays() throws BusinessException {

		try {

			return leaveRepository.getHolidays();
		} catch (Exception e) {
			throw new BusinessException(e, e.getMessage());
		}

	}

	@Override
	public void addholiday(String date, String description, boolean isNationalHoliday)
			throws BusinessException {
		
		try {

			leaveRepository.addholiday(date, description, isNationalHoliday);
		} catch (Exception e) {
			throw new BusinessException(e, e.getMessage());
		}

	}

	@Override
	public void deleteHoliday(String date) throws BusinessException {

		if(PayRollUtility.compareDateWithToday(date) < 0){
			throw new BusinessException(String.format("Holiday date %s is a past date. Hence can not be deleted", date));
		}
		
		try {

			leaveRepository.deleteHoliday(date);
		} catch (Exception e) {
			throw new BusinessException(e, e.getMessage());
		}

	}
	@Override
	public HashMap<String, String> getLeaveFromMaster() throws BusinessException{

		try {

			return leaveRepository.getLeaveFromMaster();
		} catch (Exception e) {
			throw new BusinessException(e, e.getMessage());
		}

	}

}
